<title>添加部门</title>
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a><cite>公司管理</cite></a>
        <a href="#/company/department/"><cite>部门设置</cite></a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">添加部门</div>
        <div class="layui-card-body">
            <div class="layui-form"  lay-filter="company-department-add-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">上级部门</label>
                    <div class="layui-input-inline">            
                        <input type="text" id="company-department-add-form-parentid" lay-filter="company-department-add-form-parentid" name="parentid" value="" placeholder="不选择则为一级部门" autocomplete="off" class="layui-input">           
                    </div>
                    <div class="layui-form-mid layui-word-aux">默认为公司一级部门</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="layui-bg-red">*</span>部门名称</label>
                    <div class="layui-input-inline">            
                        <input type="text" name="name" value="" lay-verify="required" placeholder="请输入部门名称" autocomplete="off" class="layui-input">           
                    </div>        
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序权重</label>
                    <div class="layui-input-inline">            
                        <input type="text" name="listorder" value="50" lay-verify="number" placeholder="数字越大排序越靠前..." autocomplete="off" class="layui-input">           
                    </div>        
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"></label>
                    <div class="layui-input-inline">
                        <input type="button" lay-submit lay-filter="company-department-add-form-submit" value="确认" class="layui-btn">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    layui.use(['admin', 'form', 'treeSelect'], function(){
        var $ = layui.jquery,
            admin = layui.admin,
            treeSelect = layui.treeSelect,
            form = layui.form;

        //树形下拉选择器 [https://fly.layui.com/extend/treeSelect/#doc]
        treeSelect.render({
            // 选择器
            elem: '#company-department-add-form-parentid',
            // data分为两种：1.数组数据 或者 2.URL地址
            //数组数据示例 [{"id": 1, "name": "财务部","open": true, "children": []}]
            data: layui.cache.baseapi + '/api/company.department/getdepartmenttree', //接口
            // 异步加载方式：get/post，默认get
            type: 'get',
            // 占位符
            placeholder: '不选择则为一级部门',
            // 是否开启搜索功能：true/false，默认false
            search: true,
            // 点击回调
            click: function(d){
                $("#company-department-add-form-parentid").val(d.current.id);
            },
            // 加载完成后的回调函数
            success: function (d) {
                var treeObj = treeSelect.zTree('company-department-add-form-parentid');
                var nodes = treeObj.getNodes();
                //默认选中主节点
                treeSelect.checkNode('company-department-add-form-parentid', 1);
                $("#company-department-add-form-parentid").val(1);
                if (nodes.length == 0) {
                    $(treeObj.setting.treeObj.selector).parent().css('display', 'none');
                }
            }
        });

        //监听提交
        form.on('submit(company-department-add-form-submit)', function (data) {                                
            admin.req({
                url: layui.cache.baseapi + '/api/company.department/add',
                type: 'POST',
                data: data.field,
                done: function(res) {
                    form.val("company-department-add-form", {//重置表单
                        "name": "",
                        "listorder": 50
                    });
                    //添加新增部门
                    var treeObj = treeSelect.zTree('company-department-add-form-parentid');
                    var parentNode = treeObj.getSelectedNodes();
                    if (!$.isEmptyObject(parentNode) && typeof parentNode[0].id != "undefined" && parentNode[0].id > 0) {
                        treeObj.addNodes(parentNode[0], -1, res.data);
                    }else{
                        treeObj.addNodes(null, -1, res.data);
                    }
                    //默认选中主节点
                    treeSelect.checkNode('company-department-add-form-parentid', 1);
                    $("#company-department-add-form-parentid").val(1);
                    layer.msg(res.msg, {icon: 1 });
                }
            });   
        });
    });
</script>